package ncu.me.musicplayer;

import android.util.Log;

/**
 * @Author ye
 * @Date 2020/6/2 12:48
 * @Version 1.0
 */
public class SeekListenThread{
    Thread mThread;
    //是否结束
    volatile boolean isFinished;
    //是否正在播放
    volatile boolean isRunning;
    Runnable mRunnable;
    public SeekListenThread(Runnable runnable){
        mRunnable = runnable;
    }
    private void init(){
        mThread = new Thread(new Runnable() {
            @Override
            public void run() {
                while (!isFinished){
                    while (isRunning){
                        if (isFinished){
                            break;
                        }
                        mRunnable.run();
                    }
                    Thread.yield();
                }
                Log.e("Thread","end");
            }
        });
    }
    public synchronized void pause(){
        isRunning = false;
    }
    public synchronized void resume(){
        isRunning = true;
    }
    public synchronized void start(){
        isFinished = false;
        isRunning = true;
        init();
        mThread.start();
    }
    public synchronized void stop(){
        isFinished = true;
        mThread = null;
    }
}
